<template>
    <div>
        <h2 style="text-align: center;">修改理财信息</h2>
        <table class="table-bordered" border="1">
            <tr>
                <td>理财产品名称</td>
                <td>
                    <input type="text" v-model="info.F_Name" placeholder="请输入理财产品名称">
                </td>
            </tr>
    
            <tr>
                <td>年收益</td>
                <td>
                    <input type="text" v-model="info.F_APY" placeholder="请输入理财产品名称">
                </td>
            </tr>
    
            <tr>
                <td>最低金额</td>
                <td>
                    <input type="text" v-model="info.F_MinMoney" placeholder="请输入最低金额">
                </td>
            </tr>

            <tr>
                <td>理财利息</td>
                <td>
                    <select v-model="info.I_Id">
                        <option value="-1">--请选择--</option>
                        <option v-for="item in SelInfo" :value="item.I_Id">{{ item.I_Year }}年利息{{ item.I_Interest }}%</option>
                    </select>
                </td>
            </tr>
    
            <tr>
                <td>风险等级</td>
                <td>
                    <input type="radio" value="0" name="Flevel" v-model="info.F_Level" checked>低风险
                    <input type="radio" value="1" name="Flevel" v-model="info.F_Level">中风险
                    <input type="radio" value="2" name="Flevel" v-model="info.F_Level">高风险
                </td>
            </tr>
    
            <tr>
                <td colspan="2" style="text-align: center;">
                    <input type="button" value="修改" class="btn btn-success" @click="UpdateFinan">
                </td>
            </tr>
        </table>
    </div>
    </template>

<script setup lang="ts">
import axios from 'axios';
import {ref,onMounted} from 'vue';
import { useRoute } from 'vue-router';
import { useRouter } from 'vue-router';

//创建路由对象实例
const route=useRoute();
const router=useRouter();

//创建响应式数据
const info=ref({
    F_Name:"",
    F_APY:"",
    F_MinMoney:"",
    I_Id:0,
    F_Level:""
})

//创建响应式数据 用于绑定下拉框
const SelInfo=ref([{
    I_Id:0,
    I_Year:0,
    I_Interest:0
}])

//创建生命周期函数钩
onMounted(()=>{
    LoadSelete()
    LoadSingle()
})

//完成下拉框加载数据
const LoadSelete=()=>{
    axios.get("https://localhost:44321/api/Finans/ShowInterest")
    .then(res=>{
        SelInfo.value=res.data
    })
}

//完成信息反填
const LoadSingle=()=>{
    axios.get("https://localhost:44321//api/Finans/SingleFinanInfo",
        {
            params:{
                Fid:route.params.Fid
            }
        }
    )
    .then(res=>{
        info.value=res.data
    })
}

//完成修改理财信息
const UpdateFinan=()=>{
    axios.put("https://localhost:44321//api/Finans/UpdateFinanInfo",
        {
            F_Id:route.params.Fid,
            F_Name:info.value.F_Name,
            F_APY:info.value.F_APY,
            F_MinMoney:info.value.F_MinMoney,
            F_Level:info.value.F_Level,
            I_Id:info.value.I_Id
        }
    )
    .then(res=>{
        if(res.data>0)
        {
            alert('理财信息修改成功');
            router.push("/ShowFinaninfo")
        }
        else if(res.data==-1)
        {
            alert('已有人进行理财,无法修改');
        }
        else
        {
            alert('理财信息修改失败！！！');
        }
    })
}
</script>